package com.jzo2o.foundations.job;

import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.service.IHomeService;
import com.jzo2o.foundations.service.IRegionService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class SampleXxlJob {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private IRegionService iRegionService;

    @Resource
    private IHomeService iHomeService;

    @XxlJob(value = "activeRegionCacheSync")
    public void activeRegionCacheSync(){
        //1.删除已有的缓存
        String key = RedisConstants.CacheName.JZ_CACHE + "::ACTIVE_REGIONS";
        redisTemplate.delete(key);
        //2.重新加载最新的缓存
        List<RegionSimpleResDTO> regionSimpleResDTOS = iRegionService.queryActiveRegionList();
        //3.遍历区域对该区域下的服务类型进行缓存
        regionSimpleResDTOS.stream().forEach(regionSimpleResDTO -> {
            Long id = regionSimpleResDTO.getId();
            String s = RedisConstants.CacheName.SERVE_ITEM + "::" + id;
            redisTemplate.delete(s);
            iHomeService.firstPageServeList(id);
        });
    }
}
